/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.intec.matsab.xml;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author matilde
 */
public class PensumXML {

    private Element pensumElemento;
    private List<TrimestreXML> listTrimestres;

    public PensumXML() {
    }

    public PensumXML(List<TrimestreXML> listTrimestres) {
        this.listTrimestres = listTrimestres;
    }

    public void setListTrimestres(List<TrimestreXML> listTrimestres) {
        this.listTrimestres = listTrimestres;
    }

    public List<TrimestreXML> getListTrimestres() {
        return listTrimestres;
    }

    public Element toXML() {
        pensumElemento = new Element("pensum");
        Iterator itr = listTrimestres.iterator();

        while (itr.hasNext()) {
            TrimestreXML trimestre = (TrimestreXML) itr.next();
            pensumElemento.addContent(trimestre.toXML());
        }

        return pensumElemento;
    }

    public byte[] getXML() throws IOException {
        toXML();
        Document myDocument = new Document(pensumElemento);

        XMLOutputter outputter = new XMLOutputter();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        outputter.output(myDocument, stream);

        byte[] bytes = stream.toByteArray();

        return bytes;
    }

    public static PensumXML toBean(byte[] buffer) {
        PensumXML pensumXml = null;

        if (buffer.length==0) {
            return pensumXml;
        }

        try {
            ByteArrayInputStream input = new ByteArrayInputStream(buffer);
            SAXBuilder builder = new SAXBuilder();
            Document doc = builder.build(input);
            Element raiz = doc.getRootElement();
            Iterator iteradorTrim = raiz.getChildren().iterator();
            List<TrimestreXML> listTrimestres = new ArrayList<TrimestreXML>();

            while (iteradorTrim.hasNext()) {
                Element trimestre = (Element) iteradorTrim.next();
                Iterator iteradorAsig = trimestre.getChildren("asignatura").iterator();
                List<AsignaturaXML> listAsignaturas = new ArrayList<AsignaturaXML>();

                while (iteradorAsig.hasNext()) {
                    Element asignatura = (Element) iteradorAsig.next();
                    String clave = asignatura.getChild("clave").getValue();
                    String descripcion = asignatura.getChild("descripcion").getValue();
                    String creditos = asignatura.getChild("creditos").getValue();

                    listAsignaturas.add(new AsignaturaXML(clave, descripcion, creditos));
                }

                listTrimestres.add(new TrimestreXML(listAsignaturas));
            }

            pensumXml = new PensumXML(listTrimestres);

        } catch (JDOMException ex) {
            Logger.getLogger(PensumXML.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PensumXML.class.getName()).log(Level.SEVERE, null, ex);
        }

        return pensumXml;
    }
}
